package com.erp.erp_servers.InspectCargo.imp;

import com.erp.erp_dao.InspectCargo.IInspectCargoDAO;
import com.erp.erp_dao.InspectCargo.IInspectCargoFileDAO;
import com.erp.erp_dao.InspectCargo.IInspectCargoProductDAO;
import com.erp.erp_dao.ShipmentPlan.IShipmentPlanDAO;
import com.erp.erp_entitys.InspectCargo.*;
import com.erp.erp_entitys.ShipmentPlan.ShipmentPlan;
import com.erp.erp_entitys.ShipmentPlan.ShipmentPlanVo;
import com.erp.erp_servers.InspectCargo.IInspectCargoService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ImpInspectCargoService implements IInspectCargoService {
    @Autowired
    private IInspectCargoDAO inspectCargoDAO;
    @Autowired
    private IInspectCargoProductDAO productDAO;
    @Autowired
    private IInspectCargoFileDAO fileDAO;
    @Autowired
    private IShipmentPlanDAO shipmentPlanDAO;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        InspectCargoEntity inspectCargo = inspectCargoDAO.selectInfoById(id);
        if(inspectCargo==null){return 0;}
        if(!CollectionUtils.isEmpty(inspectCargo.getProductList())){
            List<Integer> idList = new ArrayList<Integer>();
            for (InspectCargoProductEntity dbProduct : inspectCargo.getProductList()){
                idList.add(dbProduct.getId());
            }
            if (idList.size()>0){
                productDAO.deleteByIdList(idList);
            }
        }
        if(!CollectionUtils.isEmpty(inspectCargo.getFileList())){
            List<Integer> idList = new ArrayList<Integer>();
            for (InspectCargoFileEntity dbFile : inspectCargo.getFileList()){
                idList.add(dbFile.getId());
            }
            if (idList.size()>0){
                fileDAO.deleteByIdList(idList);
            }
        }
        return inspectCargoDAO.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(InspectCargoEntity record) {
        return inspectCargoDAO.insert(record);
    }

    @Override
    public int insertSelective(InspectCargoEntity record) {
        return inspectCargoDAO.insertSelective(record);
    }

    @Override
    public InspectCargoEntity selectByPrimaryKey(Integer id) {
        return inspectCargoDAO.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(InspectCargoEntity record) {
        return inspectCargoDAO.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(InspectCargoEntity record) {
        return inspectCargoDAO.updateByPrimaryKey(record);
    }

    @Override
    public PageInfo<InspectCargoEntity> selectInspectCargoList(InspectCargoVo icVo) {
        List<InspectCargoEntity> inspectCargoList = inspectCargoDAO.selectInspectCargoList(icVo);
        //获取分页信息
        PageInfo pageInfo = new PageInfo(inspectCargoList);
        if(CollectionUtils.isEmpty(inspectCargoList)){
            pageInfo.setList(null);
            return pageInfo;
        }
        pageInfo.setList(inspectCargoList);
        return pageInfo;
    }

    @Override
    public List<InspectCargoEntity> selectByIdList(List<String> list) {
        return inspectCargoDAO.selectByIdList(list);
    }

    @Override
    public int updateBoxStatus(List<InspectCargoEntity> list) {
        return inspectCargoDAO.updateBoxStatus(list);
    }

    @Override
    public int updateStageStatus(List<InspectCargoEntity> list) {
        return inspectCargoDAO.updateStageStatus(list);
    }

    @Override
    public int updateBoxStatusBykey(InspectCargoVo icVo) {
        return inspectCargoDAO.updateBoxStatusBykey(icVo);
    }

    @Override
    public int insertProAndFile(InspectCargoEntity record) {
        if(record==null){return 0;}
        int num = inspectCargoDAO.insert(record);
        if(num>0){
            List<InspectCargoProductEntity> productList = record.getProductList();
            if (!CollectionUtils.isEmpty(productList)){
                for(InspectCargoProductEntity pro : productList){
                    pro.setInspectCargoId(record.getId());
                    productDAO.insert(pro);
                }
            }
            List<InspectCargoFileEntity> fileList = record.getFileList();
            if (!CollectionUtils.isEmpty(fileList)){
                for(InspectCargoFileEntity file : fileList){
                    file.setInspectCargoId(record.getId());
                    file.setFileExecutor(record.getExecutor());
                    String fileName = file.getFileName();
                    if(!StringUtils.isEmpty(fileName)){
                        if (StringUtils.isEmpty(file.getFileType())){
                            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
                            file.setFileType(suffix);
                        }
                    }
                    file.setFileUploadTime(new Date(System.currentTimeMillis()));
                    fileDAO.insert(file);
                }
            }
        }
        return num;
    }

    @Override
    public int updateInfoById(InspectCargoEntity record){
        if (record==null || null == record.getId() || "".equals(record.getId())){
            return 0;
        }
        int num = inspectCargoDAO.updateByPrimaryKey(record);
        if(num>0){
            List<InspectCargoProductEntity> productList = record.getProductList();
            List<InspectCargoProductEntity> dbProductList = productDAO.selectProductByIcId(record.getId());
            if (!CollectionUtils.isEmpty(dbProductList)){
                List<Integer> idList = new ArrayList<Integer>();
                for (InspectCargoProductEntity dbProduct : dbProductList){
                    idList.add(dbProduct.getId());
                }
                if (idList.size()>0){
                    productDAO.deleteByIdList(idList);
                }
            }
            if (!CollectionUtils.isEmpty(productList)){
                for (InspectCargoProductEntity pro : productList){
                    pro.setInspectCargoId(record.getId());
                    productDAO.insert(pro);
                }
            }
            List<InspectCargoFileEntity> fileList = record.getFileList();
            List<InspectCargoFileEntity> dbFileList = fileDAO.selectFileByIcId(record.getId());
            if (!CollectionUtils.isEmpty(dbFileList)){
                List<Integer> idList = new ArrayList<Integer>();
                for (InspectCargoFileEntity dbFile : dbFileList){
                    idList.add(dbFile.getId());
                }
                if (idList.size()>0){
                    fileDAO.deleteByIdList(idList);
                }
            }
            if (!CollectionUtils.isEmpty(fileList)){
                for (InspectCargoFileEntity file : fileList){
                    file.setInspectCargoId(record.getId());
                    file.setFileExecutor(record.getExecutor());
                    String fileName = file.getFileName();
                    if(!StringUtils.isEmpty(fileName)){
                        if (StringUtils.isEmpty(file.getFileType())){
                            String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
                            file.setFileType(suffix);
                        }
                    }
                    file.setFileUploadTime(new Date(System.currentTimeMillis()));
                    fileDAO.insert(file);
                }
            }
        }
        return num;
    }

    @Override
    public boolean isExist(String oddNumber) {
        List<InspectCargoEntity> existList = inspectCargoDAO.isExist(oddNumber);
        return CollectionUtils.isEmpty(existList)?false:true;
    }

    @Override
    public String getLatestOddNumber() {
        return inspectCargoDAO.getLatestOddNumber();
    }

    @Override
    public boolean selectInspectCargoListByShipmentNo(String loadPlanNo) {
        if(!StringUtils.isEmpty(loadPlanNo)){
            InspectCargoEntity ic= inspectCargoDAO.selectUsedInspectCargoListByShipmentNo(loadPlanNo);
            if(ic!=null){
                return true;
            }
        }
        return false;
    }

    @Override
    public int generateInspectCargoByShipmentNo(String loadPlanNo) {
        int num = inspectCargoDAO.generateInspectCargoByShipmentNo(loadPlanNo);
        InspectCargoEntity ic= inspectCargoDAO.selectUnusedInspectCargoListByShipmentNo(loadPlanNo);
        if(ic!=null){
            num = productDAO.generateInspectCargoProductByShipmentPlanId(ic.getId(), loadPlanNo);
        }
        if(num>0){num=1;}
        return num;
    }

    @Override
    @Transactional
    public int delInspectCargoByLoadPlanNo(String loadPlanNo) {
        int num = 0;
        InspectCargoEntity ic= inspectCargoDAO.selectUnusedInspectCargoListByShipmentNo(loadPlanNo);
        //已删除待验货
        if(ic==null){return 1;}
        List<InspectCargoProductEntity> inspectCargoProductList = productDAO.selectProductByIcId(ic.getId());
        if(!CollectionUtils.isEmpty(inspectCargoProductList)){
            List<Integer> idList = new ArrayList<Integer>();
            for (InspectCargoProductEntity dbProduct : inspectCargoProductList){
                idList.add(dbProduct.getId());
            }
            if (idList.size()>0){
                num = productDAO.deleteByIdList(idList);
            }
        }
        if(num>0){
            num = inspectCargoDAO.deleteByPrimaryKey(ic.getId());
        }
        return num;
    }

    @Override
    public List<InspectCargoAndProduct> selectInspectCargoAndProductList(InspectCargoVo vo) {
        return inspectCargoDAO.selectInspectCargoAndProductList(vo);
    }

}
